package org.sais.eswn.card.cardlabel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import org.sais.eswn.tool.Tools;
import org.sais.eswn.card.Card;

/**
 *
 * @author Romulus
 */
public abstract class CardLabel extends JLabel implements Cloneable {

    public static final int COST_TYPE_MP = 0;
    public static final int COST_TYPE_HP = 1;
    public static final int COST_TYPE_DECK = 2;
    public static final int COST_TYPE_OTHERS = 3;
    public String cardno = "";
    public int controller;
    protected ICardLabelCallback parent;
    protected JPopupMenu mMenu;
    protected StandardMouseListener stdml = new StandardMouseListener();
    public static final int MENUSHIFT = 0;
    protected boolean isWatcher;

    public CardLabel(final ICardLabelCallback parent, String cardno, int controller, boolean isWatcher) {
        this.parent = parent;
        this.cardno = cardno;
        this.controller = controller;
        this.isWatcher = isWatcher;
        if (getInfo() != null) {
            this.setToolTipText(getInfo().name);
        } else {
            this.setToolTipText(null);
        }
    }

    public Card getInfo() {
        return Tools.getCard(cardno);
    }

    public UniLabel toUni() {
        return new UniLabel(parent, cardno, isWatcher);
    }

    public void doMouseEvent(MouseEvent evt) {
        processMouseEvent(evt);
    }

    @Override
    public CardLabel clone() {
        CardLabel clo = null;
        try {
            clo = (CardLabel) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return clo;
    }

    protected class StandardMouseListener extends MouseAdapter {

        @Override
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            try {
                if (parent == null) {
                    return;
                }
                parent.showCard(cardno);

                if (evt.getButton() != MouseEvent.BUTTON1 || evt.isControlDown()) {
                    if (mMenu != null) {
                        mMenu.show(evt.getComponent(), evt.getX(), evt.getY() - MENUSHIFT);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public static class CardTypeMismatchException extends Exception {

        CardTypeMismatchException(String message) {
            super(message);
        }
    }

    public enum Owner {

        HOST,
        CLIENT,
        NOMATTER
    }
}

